package com.donger.business.upms.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.donger.business.upms.entity.SysLabel;
import com.donger.business.upms.service.SysLabelService;
import com.donger.common.core.utils.Res;
import com.donger.common.core.utils.Result;
import com.donger.common.data.entity.DropDown;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 7326
 */
@RestController
@RequestMapping("/sys/label")
@Api(tags = "标签管理开放API")
public class SysLabelController {
    @Autowired
    private SysLabelService sysLabelService;


    /**
     * 查询某个类型的标签列表
     * @param page
     * @param type
     * @return
     */
    @GetMapping("/{type}/page")
    @ApiOperation(value = "标签管理分页查询")
    public Result page(Page<SysLabel> page,@ApiParam(value = "字典类型") @PathVariable("type") String type){
        sysLabelService.page(page, Wrappers.<SysLabel>lambdaQuery().eq(SysLabel::getType,type));
        return Res.ok(page);

    }
    @GetMapping("/{type}/dropsown/")
    @ApiOperation(value = "标签下拉框")
    public Result dorpdown(@ApiParam(value = "字典类型") @PathVariable("type") String type){

        List<SysLabel> list = sysLabelService.list(Wrappers.<SysLabel>lambdaQuery().eq(SysLabel::getType, type));
        List<DropDown> collect = list.stream().map(item -> new DropDown().setValue(item.getLabel()).setValue(item.getLabel()))
                .collect(Collectors.toList());
        return Res.ok(collect);
    }

    @GetMapping("/info/{id}")
    @ApiOperation(value = "标签详情")
    public Result info(@ApiParam(value = "标签id") @PathVariable("id")Long id){
        SysLabel label = sysLabelService.getById(id);
        return Res.ok(label);
    }

    @PostMapping("/save")
    @ApiOperation(value = "增加一个标签")
    public Result save(@RequestBody SysLabel entity){
        sysLabelService.save(entity);
        return Res.ok();
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新一个标签")
    public Result update(@RequestBody SysLabel entity){
        sysLabelService.save(entity);
        return Res.ok();
    }

    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "删除一个标签")
    public Result del(@PathVariable("id") Long id){
        sysLabelService.removeById(id);
        return Res.ok();
    }
}
